import 'dart:convert' show json;

T asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }

  return null;
}

class MaterialIndexBean {
  MaterialIndexBean({
    this.code,
    this.data,
  });

  factory MaterialIndexBean.fromJson(Map<String, dynamic> jsonRes) => jsonRes == null
      ? null
      : MaterialIndexBean(
    code: asT<int>(jsonRes['code']),
    data: Data.fromJson(asT<Map<String, dynamic>>(jsonRes['data'])),
  );

  int code;
  Data data;

  Map<String, dynamic> toJson() => <String, dynamic>{
    'code': code,
    'data': data,
  };
  @override
  String toString() {
    return json.encode(this);
  }
}

class Data {
  Data({
    this.banner,
    this.category,
  });

  factory Data.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }

    final List<Banner> banner = jsonRes['banner'] is List ? <Banner>[] : null;
    if (banner != null) {
      for (final dynamic item in jsonRes['banner']) {
        if (item != null) {
          banner.add(Banner.fromJson(asT<Map<String, dynamic>>(item)));
        }
      }
    }

    final List<Category> category =
    jsonRes['category'] is List ? <Category>[] : null;
    if (category != null) {
      for (final dynamic item in jsonRes['category']) {
        if (item != null) {
          category.add(Category.fromJson(asT<Map<String, dynamic>>(item)));
        }
      }
    }
    return Data(
      banner: banner,
      category: category,
    );
  }

  List<Banner> banner;
  List<Category> category;

  Map<String, dynamic> toJson() => <String, dynamic>{
    'banner': banner,
    'category': category,
  };
  @override
  String toString() {
    return json.encode(this);
  }
}

class Banner {
  Banner({
    this.image,
    this.product_id,
  });

  factory Banner.fromJson(Map<String, dynamic> jsonRes) => jsonRes == null
      ? null
      : Banner(
    image: asT<String>(jsonRes['image']),
    product_id: asT<int>(jsonRes['product_id']),
  );

  String image;
  int product_id;

  Map<String, dynamic> toJson() => <String, dynamic>{
    'image': image,
    'product_id': product_id,
  };
  @override
  String toString() {
    return json.encode(this);
  }
}

class Category {
  Category({
    this.id,
    this.cover,
    this.name,
  });

  factory Category.fromJson(Map<String, dynamic> jsonRes) => jsonRes == null
      ? null
      : Category(
    id: asT<int>(jsonRes['id']),
    cover: asT<String>(jsonRes['cover']),
    name: asT<String>(jsonRes['name']),
  );

  int id;
  String cover;
  String name;

  Map<String, dynamic> toJson() => <String, dynamic>{
    'id': id,
    'cover': cover,
    'name': name,
  };
  @override
  String toString() {
    return json.encode(this);
  }
}
